package com.bdf.ipharma.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.bdf.ipharma.starter.McdError;
import com.bdf.ipharma.starter.McdPrs;

/**
 * A Swing-based top level window class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUIRecharge extends GUIOperation {

   /**
    * The Layouts.
    */
   GridBagLayout layoutBase = new GridBagLayout();

   BorderLayout layoutTask = new BorderLayout();


   /**
    * The main panel.
    */
   GUIButtonPanel jPanelButtons;

   GUITransactionRecharge jPanelTransactions;

   GUIBarCode jPanelBarCode;

   GUIButtonImage jButtExtract;

   // JPanel jPanelBlank;
   JPanel jPanelTitle = new JPanel();

   JPanel jPanelTasks = new JPanel();

   JPanel jPanelTaskList = new JPanel();

   JScrollPane jTasksPane = new JScrollPane();

   GUIButtonImage jButtOk;

   int li_totalqty = 0;

   /**
    * Attributes
    */
   Vector vTasks;


   /**
    * Constructs a new instance.
    */
   public GUIRecharge(GUIWindow pContainer) {

      super(pContainer);
      try {
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      this.setLayout(layoutBase);
      this.setBackground(Colors.RCE_BACK);

      // inner class for special titles
      class MyTitle extends JLabel {

         MyTitle(String pTit, int pLength) {

            setText(pTit);
            setFont(Texts.fntTaskTitle);
            setForeground(Colors.RCE_TITLE_TEXT);
            setHorizontalAlignment(SwingConstants.CENTER);
            setMaximumSize(new Dimension(pLength, 20));
            setPreferredSize(new Dimension(pLength, 20));

         }
      }
      // titles panel
      MyTitle jLblDesc = new MyTitle(Texts.RCE_TIT_DESC, 220);
      MyTitle jLblBatch = new MyTitle(Texts.RCE_TIT_BATCH, 50);
      MyTitle jLblOrigin = new MyTitle(Texts.RCE_TIT_SRC, 90);
      MyTitle jLblQty = new MyTitle(Texts.RCE_TIT_QTY, 50);
      MyTitle jLblOk = new MyTitle(Texts.RCE_TIT_DONE, 110);
      MyTitle jLblBlank = new MyTitle(" ", 40);
      jPanelTitle.add(jLblDesc, null);
      jPanelTitle.add(jLblBatch, null);
      jPanelTitle.add(jLblOrigin, null);
      jPanelTitle.add(jLblQty, null);
      jPanelTitle.add(jLblOk, null);
      jPanelTitle.add(jLblBlank, null);
      jPanelTitle.setBackground(Colors.RCE_TITLE);

      // tasks panel
      jPanelTasks.setLayout(layoutTask);
      jPanelTasks.setBackground(Colors.RCE_BACK);
      // columns titles
      jPanelTasks.add(jPanelTitle, BorderLayout.NORTH);
      // scroll pane
      jPanelTasks.add(jTasksPane, BorderLayout.CENTER);
      jPanelTaskList.setBackground(Colors.RCE_BACK);
      //jPanelTaskList.setLayout(layoutTaskList);
      jTasksPane.getViewport().add(jPanelTaskList, null);
      jTasksPane.getVerticalScrollBar()
            .setPreferredSize(new Dimension(40, 200));
      jTasksPane.getVerticalScrollBar().setUnitIncrement(40);
      jTasksPane.getVerticalScrollBar().setSize(100, 100);

      // buttons
      jButtOk = new GUIButtonImage(Images.buttUpdate, Images.buttUpdateDn);
      jButtOk.addActionListener(new GUIRecharge_jButtOk_actionAdapter(this));
      jButtExtract = new GUIButtonImage(Images.buttExtract,
            Images.buttExtractDn);
      jButtExtract
            .addActionListener(new GUIRecharge_jButtExtract_actionAdapter(this));

      // panel: Transactions
      jPanelTransactions = new GUITransactionRecharge(this);
      jPanelTransactions.setRequestFocusEnabled(false);

      // panel: BarCode
      jPanelBarCode = new GUIBarCode(this);
      jPanelBarCode.requestFocus();
      jPanelBarCode.setActive(false);

      this.add(jPanelTasks, new GridBagConstraints2(0, 0, 1, 3, 5.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH,
            Texts.panelMargin, 0, 0));
      this.add(jButtOk, new GridBagConstraints2(1, 0, 1, 1, 0.1, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.BOTH, Texts.buttMargin,
            0, 0));
      this.add(jPanelTransactions, new GridBagConstraints2(1, 1, 1, 1, 0.1,
            1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH,
            Texts.transMargin, 0, 0));
      this.add(jButtExtract, new GridBagConstraints2(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.BOTH, Texts.buttMargin,
            0, 0));
      this.add(jPanelBarCode, new GridBagConstraints2(1, 3, 1, 1, 0.1, 0.0,
            GridBagConstraints.SOUTH, GridBagConstraints.BOTH,
            Texts.barCodeMargin, 0, 0));

   }

   /**
    * Starts execution
    */
   public void start() {

      try {
         // sets status bar message
         ((GUIMenu) myContainer).setStatusMsg(Texts.RCE_STS);

         if (vTasks == null) {
            this.populateList();
         }

         // sets focus on bar code detector text field
         jPanelBarCode.setFocus();
         this.setVisible(true);
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Populates the JList with task list values
    */
   public void populateList() {


   }

   /**
    * Returns the type of operation
    * 
    * @return type of operation
    */
   public int getType() {

      return McdPrs.rechargeId;
   }

   void jButtExtract_actionPerformed(ActionEvent e) {

      downBasket();
   }

   void downBasket() {

      // calls dispenser interface and extracts products in transaction
      try {
         myContainer.stopTimer();
         myContainer.startTimer();
      } catch (Exception ex) {
         McdError.add(ex);
      }
   }

   // updates task done
   void jButtOk_actionPerformed(ActionEvent e) {

      myContainer.kickTimer();

      

   }

}

class GUIRecharge_jButtOk_actionAdapter implements
      java.awt.event.ActionListener {

   GUIRecharge adaptee;

   GUIRecharge_jButtOk_actionAdapter(GUIRecharge adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButtOk_actionPerformed(e);
   }
}

class GUIRecharge_jButtExtract_actionAdapter implements
      java.awt.event.ActionListener {

   GUIRecharge adaptee;

   GUIRecharge_jButtExtract_actionAdapter(GUIRecharge adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButtExtract_actionPerformed(e);
   }
}
